﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace Common.Extensions
{
    public static class BMPASByte
    {
        /// <summary>
        /// giebt ein bitmap als binary array zurück
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static byte[] AsBytes(this Bitmap bmp)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, ImageFormat.Bmp);
                return ms.ToArray();
            }
            catch (Exception)
            {
                return (new byte[] { });
            }
        }

        /// <summary>
        /// erstellt aus einem bytes array ein bitmap
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Bitmap FromBytes(byte[] b)
        {
            try
            {
                return (Bitmap)Bitmap.FromStream(new MemoryStream(b));
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
